package com.le.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.le.edu.domain.entity.Section;
import com.le.edu.domain.screen.MonthVO;
import com.le.edu.domain.vo.SectionVO;
import com.le.edu.mapper.SectionMapper;
import com.le.edu.service.SectionService;
import com.le.edu.service.VideoService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 17447
 * @description 针对表【t_section(课时表)】的数据库操作Service实现
 * @createDate 2024-04-10 10:35:09
 */
@Service
public class SectionServiceImpl extends ServiceImpl<SectionMapper, Section>
        implements SectionService {

    @Lazy
    @Resource
    private VideoService videoService;

    @Override
    public List<SectionVO> selectSectionVo(Long courseId) {
        return baseMapper.selectSectionAndVideo(courseId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeSectionAndVideo(Long sectionId, Long videoId) {
        // 查询课时的视频是否在其他地方使用
        Long count = baseMapper.selectCount(new LambdaQueryWrapper<Section>()
                .eq(Section::getVideoId, videoId)
                .ne(Section::getSectionId, sectionId));
        if (count == 0) {
            // 删除视频表已以及minio视频
            videoService.removeTableAndVodById(videoId);
        }
        //删除课时
        baseMapper.deleteById(sectionId);


    }

    @Override
    public List<MonthVO> computeMonthCount() {
        return baseMapper.computeMonthCount();
    }
}




